<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item label="目录状态" prop="directory_status">
        <el-select
          v-model="queryParams.directory_status"
          clearable
          multiple
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文件状态" prop="file_status">
        <el-select
          v-model="queryParams.file_status"
          clearable
          multiple
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文件名称">
        <el-input
          v-model="queryParams.filename"
          placeholder="请输入文件名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >批量下载</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >全量下载</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="fileList"
      row-key="id"
      lazy
      :load="onload"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @row-contextmenu="showContextMenu"
      @selection-change.stop="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件名" prop="name" />
      <el-table-column label="状态" prop="status" />
      <el-table-column label="文件大小" prop="status" />
      <el-table-column label="更新时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.update_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="版本" prop="version" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <ul id="menu">
      <li v-for="item in menuList" :key="item.type" @click="handleFile(item.type)">{{ item.name }}</li>
    </ul>
  </div>
</template>

<script>
import { listRole, getRole, delRole, addRole, updateRole } from '@/api/system/role'

export default {
  name: 'ClientFiles',
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      fileList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        directory_status: undefined,
        file_status: undefined,
        filename: undefined
      },
      selectingRow: null,
      menuList: [
        { type: 1, name: '新增文件夹' },
        { type: 2, name: '新增文件' },
        { type: 3, name: '删除文件夹' },
        { type: 4, name: '删除文件' }
      ]
    }
  },
  created() {
    // this.getList()
    document.addEventListener('click', this.hideContextMenu)
  },
  beforeDestroy() {
    document.removeEventListener('click', this.hideContextMenu)
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true
      listRole(this.queryParams).then(response => {
        // this.fileList = response.rows
        this.fileList = [
          {
            id: 1, name: 'xxx', status: 0, hasChildren: true
          }, {
            id: 2, name: 'xxx2', status: 1
          }
        ]
        this.total = response.total
        this.loading = false
      })
    },
    onload(tree, treeNode, resolve) {
      resolve([
        {
          id: 11, name: 'xxx-1', status: 0
        },
        {
          id: 12, name: 'xxx-2', status: 1
        }
      ])
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
    },

    showContextMenu(row, column, event) {
      this.selectingRow = row
      const menu = document.querySelector('#menu')
      const sidebar = document.querySelector('.sidebar-container')
      const navbarContainer = document.querySelector('.navbar-container')
      const tabContent = document.querySelector('.el-tabs__content')
      event.preventDefault()
      menu.style.left = event.clientX - sidebar.offsetWidth + 10 + 'px'
      menu.style.top = event.clientY - navbarContainer.offsetHeight - tabContent.offsetTop + 'px'
      menu.style.display = 'block'
    },
    hideContextMenu() {
      const menu = document.querySelector('#menu')
      menu.style.display = 'none'
    },
    handleFile(type) {
      console.log(type)
      this.hideContextMenu()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/client/export', {
        ...this.queryParams
      }, `client_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style lang="scss" scoped>
#menu {
  position: absolute;
  display: none;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  background: #ffffff;
  cursor: pointer;
  color: #606266;
  width: 200px;
  border: 1px solid #e4e7ed;
  /* margin: 50px auto; */
  font-size: 13px;
  z-index: 100;
  list-style: none;
  margin: 0px;
  padding: 0px;
}

#menu li {
  padding: 0px 10px;
  height: 30px;
  line-height: 30px;
  position: relative;
  box-sizing: border-box;
  text-indent: 8px;
}
</style>
